package com.commerceE.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import com.commerceE.common.exception.ServiceException;
import com.commerceE.dao.OrderDao;
import com.commerceE.dao.OrderItemDao;
import com.commerceE.domain.Category;
import com.commerceE.domain.Order;
import com.commerceE.domain.Product_instance;
import com.commerceE.domain.Products;
import com.commerceE.domain.User;
import com.commerceE.service.CategoryService;
import com.commerceE.service.OrderService;
import com.commerceE.service.ProductService;

@SessionAttributes({"currentUser"})
@Controller
@RequestMapping("/order")
public class OrderController {
	
	@Autowired
	private ProductService productService;

	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private OrderService orderService;
	

	@ModelAttribute("currentUser")
	public User initCurrentUser(Model model) {
		Object user = RequestContextHolder.currentRequestAttributes().getAttribute("currentUser", RequestAttributes.SCOPE_SESSION);
		if(user!=null){
			return (User)user;
		}else{
		    return User.createInitalUser(); 
		}
    }
	
	@RequestMapping(value = "/orderSubmit", method = RequestMethod.POST)
	public String orderSubmit(@ModelAttribute("currentUser")User currentUser,Long orderId, Long addressId, Model model){
		Order order = null;
		if(orderId!=null && orderId!=0){
			order = orderService.getOrderById(orderId);
			order.setAddressId(addressId);
			order.setLastupdatedtime(new Date());
			orderService.updateOrder(order);
		}else{
			if(currentUser.getProductInstanceListOfSession()==null || currentUser.getProductInstanceListOfSession().size()==0){
//				result.reject("error", "Please shopping cart is null, you cannot submit this order.");
				return "checkout";
			}
		}
		order = orderService.insertOrder(currentUser, addressId);
		currentUser.setProductInstanceListOfSession( new ArrayList<Product_instance>());
		currentUser.setTotalPrice(0);
		currentUser.setTotalQuantity(0);
		if(User.validateCurrentUser(currentUser)){
			currentUser.getOrderList().add(order);
		}
		return "redirect:/order/orderList.html";
	}
	
	@RequestMapping(value = "/orderList", method = {RequestMethod.POST,RequestMethod.GET})
	public String orderList(@ModelAttribute("currentUser")User currentUser,Long addressId, Model model){
		List<Category> categoryList = null;
		List<Products> featuredProductList = null;
		List<Order> orderList = null;
		try {
			categoryList = categoryService.getAllListOfCategory();
			featuredProductList = productService.getProductListWithImage();
			if(User.validateCurrentUser(currentUser)){
				orderList = orderService.getOrderListByUserId(currentUser.getUserid());
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		
		model.addAttribute("categoryList",categoryList);
		model.addAttribute("featuredProductList",featuredProductList);
		model.addAttribute("orderList",orderList);
		return "orderList";
	}

}
